import requests
import re
import os

if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    if not os.path.exists('./qiutuLibs'):
        os.mkdir('./qiutuLibs')
    # 设置一个通用的url模板
    url= 'https://www.qiushibaike.com/imgrank/page/%d/'
    for pageNum in range(1,36):
        new_url = format(url%pageNum)

        # 使用通用爬虫对url对应的整张页面进行爬取
        page_text = requests.get(url=new_url, headers=headers).text

        # 使用聚焦爬虫将页面所有的糗图提取
        ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
        img_src_list = re.findall(ex, page_text, re.S) # 将正则作用于page_text re.S 单行匹配(数据解析只用re.S) re.M多行匹配
        for src in img_src_list:
            # 拼接出完整图片地址
            src = 'https:' + src
            # 请求到了图片的二进制数据
            img_data = requests.get(url=src, headers=headers).content
            img_name = src.split('/')[-1]
            # 图片存储的路径
            imgPath = './qiutuLibs/' + img_name
            with open(imgPath, 'wb') as fp:
                fp.write(img_data)
                print(img_name, '下载成功')